﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using TTGame;

/*
 * SimpleBullet
 * 最简单的子弹
 *
 * 和AttackFrame的区别只在于，有移动的概念。
 *
 * 依赖：
 * MagicBallManager,DamageReceiverCollider,puppet
 *
 * ！不写重复代码！
 * @author TTun
 * @date 7/27/2017 2:42:42 PM
 */
namespace TTGameEngine {

public class SimpleBullet : MagicBall {

    //从编辑器设置参数，或者技能Trigger中赋值。
    [SerializeField, ReadOnly]
    MoveInfo m_moveInfo;

    public bool isFaceToMoveDir = false;

    Vector3 t_curMoveDir = Vector3.zero;


    public override float GetMoveSpeed() {
        return m_moveInfo.moveSpeed;
    }
    public override void SetMoveSpeed(float moveSpeed) {
        m_moveInfo.moveSpeed = moveSpeed;
    }

    protected override void init(int skillID, uint castID) {
        base.init(skillID, castID);

        if(m_colInfo.m_targetType.IsID((int)TargetSelectType.Self)) {
            targetObjIsSelf = true;
        } else {
            //加载DRCollider组件
            LayerMask layerMask = ObjectManager.Share().GetObjLayerMask(m_colInfo.m_targetType, m_owner.m_objType);
            drCollider = GameExt.AddDRColliderComponent(m_colInfo, transform, gameObject, layerMask);
            drCollider.onColliderEnter += OnCollider;
            drCollider.OnHitObject += OnHitEffect;
        }
        if(isFaceToMoveDir) {
            xform.forward = m_owner.objTF.forward;
        }
    }

    public MoveInfo MoveInfo {
        get {
            return m_moveInfo;
        }
    }

    protected override void Update() {
        float dt = Time.deltaTime;
        bool result = true;
        result &= updateLife(dt);
        //飞行
        if(result) {
            result &= updateMove(dt);
        }
        if(result) {
            result &= updateColliderCount();
        }
        if(isFaceToMoveDir) {
            updateFaceDir();
        }
        if(result == false) {
            OnLifeOver();
        }
    }

    void updateFaceDir() {
        if(t_curMoveDir.IsZero() == false)
            xform.forward = t_curMoveDir;
    }

    /// <summary>
    ///
    /// </summary>
    /// <param name="dt"></param>
    /// <returns>true，继续逻辑</returns>
    bool updateMove(float dt) {
        if(GetMoveSpeed() <= 0) {
            return true;
        }
        //DebugUtil.LogFormat("UpdateMove  Time.delta[{0}]", Time.deltaTime);
        Vector3 nextPos = Vector3.zero;
        bool isMoveEnd = m_moveInfo.ObjMoveStep(dt, xform.position, out nextPos);
        t_curMoveDir = nextPos - xform.position;
        xform.position = nextPos;

        lastMoveDis = m_moveInfo.lastMoveDis;
#if UNITY_EDITOR
        if(m_colInfo.m_atkShape == ColliderShape.Line && lastMoveDis > drCollider.ColliderRage) {
            MagicBallLogWarning("子弹可能没有碰撞到怪物。  moveDis[{0}] speed[{1}] rayLenFactor[{2}]", lastMoveDis, m_colInfo.m_moveSpeed, m_colInfo.m_rayLenFactor);
        }
#endif
        return !isMoveEnd;
    }


    public override void EmitSelf(Vector3 startPos) {
        transform.parent = MagicBallManager.Share().GetRootTF();
        transform.position = startPos;
        base.EmitSelf();
    }

    public override void Fire() {
        base.Fire();
    }
    public override void DeactiveMagicBall(bool isRemove = true) {
        base.DeactiveMagicBall(isRemove);
    }
}

}
